<?php
declare (strict_types = 1);

namespace app\model;

use app\exception\ModelException;
use app\exception\ModelEmptyException;

/**
 * @mixin \think\Model
 */
class Attachment extends Model
{
    public $modelEmptyMsg = '附件内容不存在';

    public function getStorageAttr($value): string
    {
        if(empty($value)){
            return '本地';
        }
        if($value == 1){
            return '本地';
        }else if($value == 2){
            return '阿里云';
        }else if($value == 3){
            return '七牛云';
        }else{
            return '腾讯云';
        }
    }

    public function getUrlAttr($value): string
    {
        $value = str_replace('\\', '/', $value);
        if(strpos($value,'/') != 0 && $this->storage == '本地' ){
            $value = '/' .$value;
        }
        return  $value;
    }

    public function getSizeAttr($value): string
    {
        if(!empty($value) || $value === 0 ){
            return $value . 'kb';
        }else{
            return '未知';
        }
    }

    /**
     * @param array $where
     * @param int $limit
     * @return array
     * @throws ModelException
     */
    public function getAttachmentList(array $where = [],int $limit = 10): array
    {
        try{
           $res =  $this->where($where)->order('id desc')->paginate($limit);
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        return dataReturn($this->sucCode,$this->getMsg,$res);

    }

    /**
     * @param array $where
     * @return array
     * @throws ModelException
     */
    public function getAttachment(array $where = []): array
    {
        try{
           $res =  $this->where($where)->find();
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        return dataReturn($this->sucCode,$this->getMsg,$res);

    }

    /**
     * @param $param
     * @return array
     * @throws ModelException
     */
    public function addAttachment($param): array
    {
        try{
            $res =  self::create($param);
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        return dataReturn($this->sucCode,$this->addMsg,$res);
    }

    /**
     * @param array $where
     * @param array $param
     * @return array
     * @throws ModelException
     */
    public function updateAttachment(array $where = [],array $param = []): array
    {
        try{
           $res =  self::where($where)->update($param);
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        return dataReturn($this->sucCode,$this->updateMsg,$res);
    }

    /**
     * @param $where
     * @return array
     * @throws ModelException
     */
    public function softDelAttachment($where): array
    {
        try{
            $res =  $this->where($where)->update($this->delData);
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        return dataReturn($this->sucCode,$this->delMsg,$res);
    }

    /**
     * @param $where
     * @return array
     * @throws ModelException
     */
    public function delAttachment($where): array
    {
        try{
            $res =  $this->where($where)->delete();
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        return dataReturn($this->sucCode,$this->delMsg,$res);
    }
}